// pages/tabBar/my/component/msg/msg.js
import { request } from '../.././../../../utils/util'
const { $Message } = require('../../../../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageForRead: [],
    messageForUnread:[],
    count:'',
    current: 'unread',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMsg();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      this.getMsg();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取消息封装
   */
  getMsg() {
    const that = this
    request("GET", "/api/auth/message/receiver/")
      .then(res => {
        if (res.statusCode === 200) {
          const list = res.data
          let messageForRead = []
          let messageForUnread = []
          list.forEach(el =>{
           
              if(el.status === 0 && el.submit_action != 'copy'){
                messageForUnread.push(el)
              }
              if(el.status === 1 && el.submit_action != 'copy'){
                messageForRead.push(el)
              }
          })
          that.setData({
            ...that.data,
            messageForRead:messageForRead,
            messageForUnread:messageForUnread,
            count:messageForUnread.length
          })
          wx.stopPullDownRefresh();
        }
      })
  },
  /**
   * tab切换事件
   * @param {*} param0 
   */
  handleChange({ detail }) {
    this.setData({
      current: detail.key
    });
  },

  /**
   * 接受
   */
  receive(e){
    const that = this
    const id = e.target.dataset.id
    const param = {
      is_complete:1
    }
    request("PUT",`/api/auth/message/${id}/`,param)
    .then(res =>{
      if(res.statusCode === 200){
        $Message({
          content: '您已接受邀请',
          type: 'success'
      });
      that.getMsg();
      }
    })
  },
  /**
   * 拒绝
   */
  reject(e){
    const that = this
    const id = e.target.dataset.id
    const param = {
      is_complete:0
    }
    request("PUT",`/api/auth/message/${id}/`,param)
    .then(res =>{
      if(res.statusCode === 200){
        $Message({
          content: '您已拒绝邀请',
          type: 'warning'
      });
      that.getMsg();
      }
    })
  }
})